"use strict";
/*
 * @Description: 天气查询工具，根据城市名称获取天气预报信息
 * @Author: 莫若省
 * @date: 2025/9/3
 *
 * 功能说明：
 * 1. 支持通过城市名称或城市+区名称查询天气
 * 2. 返回未来多天的天气预报信息
 * 3. 包含温度、天气状况、风力、空气质量等信息
 * 4. 适配新的API响应格式
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.weather_tool = void 0;
var zod_1 = require("zod");
var axios_1 = require("axios");
// 天气查询工具生成器
var weatherToolGenerator = function () { return ({
    name: "天气查询",
    description: "根据城市名称或城市+区名称查询天气预报信息，返回未来多天的天气数据，包括温度、天气状况、风力、空气质量等",
    prams: {
        city: zod_1.z.string().describe("请输入要查询天气的城市名称或城市+区名称，例如：北京、上海市浦东新区、广州天河区等"),
    },
    cb: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
        var apiUrl, response, weatherData, error_1, errorMessage;
        var _c, _d, _e;
        var city = _b.city;
        return __generator(this, function (_f) {
            switch (_f.label) {
                case 0:
                    // 检查城市名称是否为空
                    if (!city || city.trim() === "") {
                        return [2 /*return*/, {
                                content: [{
                                        text: "请输入有效的城市名称或城市+区名称",
                                        type: "text"
                                    }]
                            }];
                    }
                    _f.label = 1;
                case 1:
                    _f.trys.push([1, 3, , 4]);
                    apiUrl = "https://api.iyuns.com/api/weather?city=".concat(encodeURIComponent(city));
                    return [4 /*yield*/, axios_1.default.get(apiUrl, {
                            timeout: 10000, // 10秒超时
                            headers: {
                                'Accept': 'application/json',
                                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                            }
                        })];
                case 2:
                    response = _f.sent();
                    // 检查响应状态和数据格式
                    if (response.status !== 200 || ((_c = response.data) === null || _c === void 0 ? void 0 : _c.code) !== 200) {
                        return [2 /*return*/, {
                                content: [{
                                        text: ((_d = response.data) === null || _d === void 0 ? void 0 : _d.msg) || "\u5929\u6C14API\u8BF7\u6C42\u5931\u8D25\uFF0C\u72B6\u6001\u7801: ".concat(response.status),
                                        type: "text"
                                    }]
                            }];
                    }
                    weatherData = response.data.data;
                    // 验证数据格式
                    if (!weatherData || !Array.isArray(weatherData.data)) {
                        return [2 /*return*/, {
                                content: [{
                                        text: "天气API返回数据格式不正确",
                                        type: "text"
                                    }]
                            }];
                    }
                    // 格式化响应数据
                    return [2 /*return*/, formatWeatherResponse(weatherData)];
                case 3:
                    error_1 = _f.sent();
                    console.error("天气查询错误:", error_1);
                    errorMessage = "天气查询失败，请稍后重试";
                    if (axios_1.default.isAxiosError(error_1)) {
                        if (error_1.code === 'ECONNABORTED') {
                            errorMessage = "天气查询请求超时，请检查网络连接";
                        }
                        else if (error_1.response) {
                            errorMessage = ((_e = error_1.response.data) === null || _e === void 0 ? void 0 : _e.msg) || "\u5929\u6C14API\u670D\u52A1\u5F02\u5E38\uFF0C\u72B6\u6001\u7801: ".concat(error_1.response.status);
                        }
                        else if (error_1.request) {
                            errorMessage = "无法连接到天气服务，请检查网络连接";
                        }
                    }
                    return [2 /*return*/, {
                            content: [{
                                    text: errorMessage,
                                    type: "text"
                                }]
                        }];
                case 4: return [2 /*return*/];
            }
        });
    }); }
}); };
// 格式化天气响应
function formatWeatherResponse(weatherData) {
    // 构建详细的天气信息文本
    var weatherText = "".concat(weatherData.city, "\u5929\u6C14\u4FE1\u606F\uFF1A\n\n");
    weatherData.data.forEach(function (day, index) {
        weatherText += "\u65E5\u671F: ".concat(day.date, "\n");
        weatherText += "\u6E29\u5EA6: ".concat(day.temperature, "\n");
        weatherText += "\u5929\u6C14: ".concat(day.weather, "\n");
        weatherText += "\u98CE\u529B: ".concat(day.wind, "\n");
        weatherText += "\u7A7A\u6C14\u8D28\u91CF: ".concat(day.air_quality, "\n");
        if (index < weatherData.data.length - 1) {
            weatherText += "\n";
        }
    });
    return {
        content: [
            {
                text: weatherText,
                type: "text"
            }
        ]
    };
}
exports.weather_tool = weatherToolGenerator;
